#!/bin/bash

cd /root/kube_master_elb_src

# 将配置信息加载到session的环境变量中
source ./config/config.ini

# 获取主机名称
LOCAL_HOST_NAMES=$(hostname)
LOCAL_HOST_NAME=${LOCAL_HOST_NAMES%%.*}

# 获取主机IP
LOCAL_HOST_IP=$1

echo "LOCAL_HOST_NAME=" $LOCAL_HOST_NAME "LOCAL_HOST_IP=" $LOCAL_HOST_IP

# 获取主机网卡名

LOCAL_HOST_ETHNAME=$(ip r show|grep $LOCAL_HOST_IP|awk '{print $3}')

# 修改 keepalived 配置

sed -i "s/CHANGE_THIS_ETHNAME/${LOCAL_HOST_ETHNAME}/g" /etc/keepalived/keepalived.conf
sed -i "s/CHANGE_THIS_VIP/${MASTER_VIP}/g" /etc/keepalived/keepalived.conf

# 修改 haproxy 配置

sed -i "s/MASTER_IP_1/${MASTER_IP_1}/g" /etc/haproxy/haproxy.cfg
sed -i "s/MASTER_IP_2/${MASTER_IP_2}/g" /etc/haproxy/haproxy.cfg
sed -i "s/MASTER_IP_3/${MASTER_IP_3}/g" /etc/haproxy/haproxy.cfg

# 启动 keepalived haproxy

systemctl daemon-relaod
systemctl restart keepalived
systemctl restart haproxy
systemctl enable keepalived
systemctl enable haproxy
